package com.withoutwords.ui.custom;

import com.withoutwords.R;
import com.withoutwords.engine.Guess;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GuessLayout extends RelativeLayout {

	private TextView txtName;
	private TextView txtGuessWord;
	private TextView txtGuessWordIndex;
	Guess guess;
	IGuessClickListener guessListener;

	public GuessLayout(Context context) {
		super(context);
		init();
	}

	public GuessLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}

	void init() {
		LayoutInflater inf = LayoutInflater.from(getContext());
		inf.inflate(R.layout.guess_layout, this);
		txtName = (TextView) findViewById(R.id.txtViewName);
		txtGuessWord = (TextView) findViewById(R.id.txtViewGuessWord);
		txtGuessWordIndex = (TextView) findViewById(R.id.txtViewGuessWordIndex);
	}

	/*
	 * public void setGuess(Guess guess) { setGuess(guess,null); }
	 */
	public int getExpectedWidth() {
		float wid = 0;

		wid = txtGuessWordIndex.getPaint().measureText(String.valueOf( guess.wordIndex+1))+
				txtGuessWord.getPaint().measureText(guess.wordGuess) + 50;// 100
																		// for
																		// buttons
																		// (approve
																		// and
																		// decline)
		return (int) wid;
	}

	public void setGuess(final Guess guess, IGuessClickListener l,
			boolean bshowApprove) {
		this.guess = guess;
		this.guessListener = l;
		if (guess.playerName != null) {
			String playerName = guess.playerName;
			int ind = guess.playerName.indexOf(' ');
			if (ind > 0) {
				playerName = guess.playerName.substring(0, ind);
			}
			txtName.setText(playerName);
		}
		txtGuessWord.setText(guess.wordGuess);
		txtGuessWordIndex.setText(String.valueOf(guess.wordIndex+1));

		if (this.guessListener != null) {
			ImageButton btnAprove = (ImageButton) findViewById(R.id.btnApprove);
			if (bshowApprove) {

				btnAprove.setVisibility(VISIBLE);
				btnAprove.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						guessListener.onActionApprovedClicked(guess);
						GuessLayout.this.setVisibility(INVISIBLE);

					}
				});
			} else {
				btnAprove.setVisibility(INVISIBLE);
			}
			ImageButton btnDecline = (ImageButton) findViewById(R.id.btnDecline);
			btnDecline.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					guessListener.onActionDeclinedClicked(guess);
					GuessLayout.this.setVisibility(INVISIBLE);

				}
			});

		}

	}
}
